package com.ssy.system.controller;

import com.ssy.common.CodeMsg;
import com.ssy.common.Constant;
import com.ssy.common.utils.MD5Utils;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import com.ssy.system.pojo.User;
import com.ssy.system.service.ICommonTypeService;
import com.ssy.system.service.IRoleService;
import com.ssy.system.service.IUserService;
import com.ssy.system.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author ssy
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ICommonTypeService commonTypeService;

    @Autowired
    private IRoleService roleService;

    /**
     * 加载用户列表
     *
     * @param userVo
     * @return
     */
    @GetMapping("/loadAllUser")
    public PageResult loadAllUser(UserVo userVo) {
        return this.userService.queryAllUserByPage(userVo);
    }

    @GetMapping("/loadUserType")
    public Result loadUserType() {
        return this.commonTypeService.queryAllCommonType(Constant.USER_TYPE);
    }

    /**
     * 加载职称下拉框列表
     *
     * @return
     */
    @GetMapping("/loadUserLevel")
    public Result loadUserLevel() {
        return this.commonTypeService.queryAllCommonType(Constant.DEP_LEVEL_TYPE);
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user) {
        String salt = MD5Utils.getSalt();
        user.setSalt(salt);
        user.setPassword(MD5Utils.md5(Constant.USER_DEFAULT_PASSWORD, salt, 4));
        user.setCreateTime(new Date());
        return this.userService.insertUser(user);
    }

    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User user) {
        return this.userService.updateUserById(user);
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteUser/{id}")
    public Result deleteUser(@PathVariable Integer id) {
        return this.userService.deleteUserById(id);
    }

    /**
     * 批量删除用户
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/bathDeleteUser/{ids}")
    public Result bathDeleteUser(@PathVariable Integer[] ids) {
        return this.userService.bathDeleteUser(ids);
    }

    /**
     * 修改用户状态
     *
     * @param id
     * @param state
     * @return
     */
    @PostMapping("/updateUserState/{id}/{state}")
    public Result updateUserState(@PathVariable Integer id, @PathVariable Integer state) {
        User user = new User();
        user.setId(id);
        user.setState(state);
        return this.userService.updateUserById(user);
    }

    /**
     * 重置用户密码
     *
     * @param ids ids
     * @return data
     */
    @GetMapping("/resetUserPassword/{ids}")
    public Result resetUserPassword(@PathVariable Integer[] ids) {
        return this.userService.resetUserPwdById(ids);
    }

    /**
     * 根据用户ID加载用户的角色
     *
     * @param id 用户id
     * @return data
     */
    @GetMapping("/loadRolesByUserId")
    public Result loadRolesByUserId(Integer id) {
        return this.roleService.queryRolesByUserId(id);
    }

    /**
     * 用户分配角色
     *
     * @param userId 用户ID
     * @param ids    角色ID
     * @return data
     */
    @PostMapping("/saveRoleUser")
    public Result saveRoleUser(Integer userId, Integer[] ids) {
        return this.userService.insertRoleUser(userId, ids);
    }


    /**
     * 注销
     *
     * @param request
     * @return
     */
    @GetMapping("/cancellation")
    public void cancellation(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getSession().removeAttribute(Constant.SESSION_CURRENT_USER);
        response.sendRedirect("/");
    }


    /**
     * 用户修改密码
     *
     * @param password     老密码
     * @param newPassword1 新密码
     * @return
     */
    @PostMapping("/updatePwd")
    public Result updatePwd(String password, String newPassword1, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(Constant.SESSION_CURRENT_USER);
        //加密之后的密码
        String pwd = MD5Utils.md5(password, user.getSalt(), 4);
        if (!pwd.equals(user.getPassword())) {
            return new Result(CodeMsg.LOGIN_UPDATE_PWD_ERROR);
        }
        String salt = MD5Utils.getSalt();
        String newPwd = MD5Utils.md5(newPassword1, salt, 4);
        User user1 = new User(user.getId(), newPwd, salt);
        int i = this.userService.updateUserPwdById(user1);
        return i > 0 ? new Result(CodeMsg.UPDATE_SUCCESS) : new Result(CodeMsg.UPDATE_ERROR);
    }

}
